/**
 * Copyright 2014,Peak Tai 台俊峰(taijunfeng_it@sina.com).
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * 		http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.cenluan.user.log;

import java.util.Date;

import com.cenluan.activity.Activity;
import com.cenluan.activity.comment.ActivityComment;
import com.cenluan.blog.Blog;
import com.cenluan.blog.comment.BlogComment;
import com.cenluan.kit.UUIDKit;
import com.cenluan.tweet.Tweet;
import com.cenluan.tweet.comment.TweetComment;
import com.cenluan.user.User;
import com.cenluan.vote.Vote;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;

/**
 * 用户日志，动态
 * 
 * @author peak
 * 
 */
public class UserLog extends Model<UserLog> {

	private static final long serialVersionUID = 1L;

	public static final UserLog dao = new UserLog();
	/**
	 * 创建投票
	 */
	public static final String TYPE_CREATE_VOTE = "CREATE_VOTE";
	/**
	 * 参与投票
	 */
	public static final String TYPE_JOIN_VOTE = "JOIN_VOTE";
	/**
	 * 相册类型的动态
	 */
	public static final String TYPE_GALLERY = "GALLERY";
	/**
	 * 微博
	 */
	public static final String TYPE_TWEET = "TWEET";
	/**
	 * 微博评论
	 */
	public static final String TYPE_TWEET_COMMENT = "TWEET_COMMENT";
	/**
	 * 博客
	 */
	public static final String TYPE_BLOG = "BLOG";
	/**
	 * 博客评论
	 */
	public static final String TYPE_BLOG_COMMENT = "BLOG_COMMENT";
	/**
	 * 活动
	 */
	public static final String TYPE_ACTIVITY = "ACTIVITY";
	/**
	 * 活动评论
	 */
	public static final String TYPE_ACTIVITY_COMMENT = "ACTIVITY_COMMENT";

	/**
	 * 获取关联用户
	 * 
	 * @return
	 */
	public User getUser() {
		return User.dao.findById(getStr("userId"));
	}

	/**
	 * 保存动态
	 * 
	 * @param userId
	 * @param type
	 * @param content
	 * @param referenceId
	 *            要引用的关联id
	 * @return
	 */
	public static boolean save(String userId, String type, String referenceId) {
		UserLog userLog = new UserLog();
		userLog.set("id", UUIDKit.generateUUID());
		userLog.set("userId", userId);
		userLog.set("type", type);
		userLog.set("createTime", new Date());
		userLog.set("referenceId", referenceId);
		return userLog.save();
	}

	/**
	 * 依据关联的引用id批量删除
	 * 
	 * @param referenceId
	 */
	public void deleteByReferenceId(String referenceId) {
		Db.update("delete from t_user_log where referenceId=?", referenceId);
	}

	/**
	 * 获取关联的投票
	 * 
	 * @return
	 */
	public Vote getVote() {
		return Vote.dao.findById(getStr("referenceId"));
	}

	/**
	 * 获取关联的微博
	 * 
	 * @return
	 */
	public Tweet getTweet() {
		return Tweet.dao.findById(getStr("referenceId"));
	}

	/**
	 * 获取关联的微博评论
	 * 
	 * @return
	 */
	public TweetComment getTweetComment() {
		return TweetComment.dao.findById(getStr("referenceId"));
	}

	/**
	 * 获取关联博客
	 * 
	 * @return
	 */
	public Blog getBlog() {
		return Blog.dao.findById(getStr("referenceId"));
	}

	/**
	 * 获取关联的博客评论
	 * 
	 * @return
	 */
	public BlogComment getBlogComment() {
		return BlogComment.dao.findById(getStr("referenceId"));
	}

	/**
	 * 获取关联的活动
	 * 
	 * @return
	 */
	public Activity getActivity() {
		return Activity.dao.findById(getStr("referenceId"));
	}

	/**
	 * 获取关联的活动评论
	 * 
	 * @return
	 */
	public ActivityComment getActivityComment() {
		return ActivityComment.dao.findById(getStr("referenceId"));
	}

}
